Filters

By Evgenia "Jenny" Nitishinskaya, Dr. Aidan O'Mahony, and Delaney Granizo-Mackenzie. Algorithms by David Edwards.

Kalman Filter Beta Estimation Example from Dr. Aidan O'Mahony's blog.

Part of the Quantopian Lecture Series:

Notebook released under the Creative Commons Attribution 4.0 License.



In [15]:
from SimPEG import *
%pylab inline
# Import a Kalman filter and other useful libraries
from pykalman import KalmanFilter
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from scipy import poly1d


Efficiency Warning: Interpolation will be slow, use setup.py!

            python setup.py build_ext --inplace
    
Populating the interactive namespace from numpy and matplotlib
WARNING: pylab import has clobbered these variables: ['linalg', 'fft']
`%matplotlib` prevents importing * from pylab and numpy

Toy example: falling ball

Imagine we have a falling ball whose motion we are tracking with a camera. The state of the ball consists of its position and velocity. We know that we have the relationship $x_t = x_{t-1} + v_{t-1}\tau - \frac{1}{2} g \tau^2$, where $\tau$ is the time (in seconds) elapsed between $t-1$ and $t$ and $g$ is gravitational acceleration. Meanwhile, our camera can tell us the position of the ball every second, but we know from the manufacturer that the camera accuracy, translated into the position of the ball, implies variance in the position estimate of about 3 meters.

In order to use a Kalman filter, we need to give it transition and observation matrices, transition and observation covariance matrices, and the initial state. The state of the system is (position, velocity), so it follows the transition matrix $$ \left( \begin{array}{cc} 1 & \tau \\ 0 & 1 \end{array} \right) $$

with offset $(-\tau^2 \cdot g/2, -\tau\cdot g)$. The observation matrix just extracts the position coordinate, (1 0), since we are measuring position. We know that the observation variance is 1, and transition covariance is 0 since we will be simulating the data the same way we specified our model. For the inital state, let's feed our model something bogus like (30, 10) and see how our system evolves.


In [16]:
tau = 0.1

# Set up the filter
kf = KalmanFilter(n_dim_obs=1, n_dim_state=2, # position is 1-dimensional, (x,v) is 2-dimensional
                  initial_state_mean=[30,10],
                  initial_state_covariance=np.eye(2),
                  transition_matrices=[[1,tau], [0,1]],
                  observation_matrices=[[1,0]],
                  observation_covariance=3,
                  transition_covariance=np.zeros((2,2)),
                  transition_offsets=[-4.9*tau**2, -9.8*tau])

In [17]:
# Create a simulation of a ball falling for 40 units of time (each of length tau)
times = np.arange(40)
actual = -4.9*tau**2*times**2

# Simulate the noisy camera data
sim = actual + 3*np.random.randn(40)

# Run filter on camera data
state_means, state_covs = kf.filter(sim)

In [18]:
plt.plot(times, state_means[:,0])
plt.plot(times, sim)
plt.plot(times, actual)
plt.legend(['Filter estimate', 'Camera data', 'Actual'])
plt.xlabel('Time')
plt.ylabel('Height');



In [19]:
print(times)


[ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39]

In [20]:
print(state_means[:,0])


[ 22.90866398  18.42013824  16.28352774  13.77538728  11.92328042
  10.35074305   8.82132951   6.65263006   5.07145031   4.5407547
   2.43346275   0.37088762  -1.14172     -3.73796763  -5.9759926
  -8.43324433 -10.69877234 -12.89537802 -15.4705315  -17.32803309
 -19.74547196 -22.58327868 -24.33176559 -26.30216201 -29.01572957
 -32.14215074 -35.0331289  -38.06921832 -40.37744035 -43.33672562
 -45.84866517 -49.17265712 -52.313737   -55.28020686 -58.05294137
 -61.86735847 -65.62830218 -69.18563567 -72.67886449 -76.49142187]

At each point in time we plot the state estimate after accounting for the most recent measurement, which is why we are not at position 30 at time 0. The filter's attentiveness to the measurements allows it to correct for the initial bogus state we gave it. Then, by weighing its model and knowledge of the physical laws against new measurements, it is able to filter out much of the noise in the camera data. Meanwhile the confidence in the estimate increases with time, as shown by the graph below:


In [21]:
# Plot variances of x and v, extracting the appropriate values from the covariance matrix
plt.plot(times, state_covs[:,0,0])
plt.plot(times, state_covs[:,1,1])
plt.legend(['Var(x)', 'Var(v)'])
plt.ylabel('Variance')
plt.xlabel('Time');


The Kalman filter can also do smoothing, which takes in all of the input data at once and then constructs its best guess for the state of the system in each period post factum. That is, it does not provide online, running estimates, but instead uses all of the data to estimate the historical state, which is useful if we only want to use the data after we have collected all of it.


In [22]:
# Use smoothing to estimate what the state of the system has been
smoothed_state_means, _ = kf.smooth(sim)

# Plot results
plt.plot(times, smoothed_state_means[:,0])
plt.plot(times, sim)
plt.plot(times, actual)
plt.legend(['Smoothed estimate', 'Camera data', 'Actual'])
plt.xlabel('Time')
plt.ylabel('Height');


Example: moving average

Because the Kalman filter updates its estimates at every time step and tends to weigh recent observations more than older ones, a particularly useful application is estimation of rolling parameters of the data. When using a Kalman filter, there's no window length that we need to specify. This is useful for computing the moving average if that's what we are interested in, or for smoothing out estimates of other quantities. For instance, if we have already computed the moving Sharpe ratio, we can smooth it using a Kalman filter.

Below, we'll use both a Kalman filter and an n-day moving average to estimate the rolling mean of a dataset. We hope that the mean describes our observations well, so it shouldn't change too much when we add an observation; therefore, we assume that it evolves as a random walk with a small error term. The mean is the model's guess for the mean of the distribution from which measurements are drawn, so our prediction of the next value is simply equal to our estimate of the mean. We assume that the observations have variance 1 around the rolling mean, for lack of a better estimate. Our initial guess for the mean is 0, but the filter quickly realizes that that is incorrect and adjusts.


In [23]:
df = pd.read_csv("../data/ChungCheonDC/CompositeETCdata.csv")
df_DC = pd.read_csv("../data/ChungCheonDC/CompositeDCdata.csv")
df_DCstd = pd.read_csv("../data/ChungCheonDC/CompositeDCstddata.csv")

In [24]:
ax1 = plt.subplot(111)
ax1_1 = ax1.twinx()
df.plot(figsize=(12,3), x='date', y='reservoirH', ax=ax1_1, color='k', linestyle='-', lw=2)


Out[24]:
<matplotlib.axes._subplots.AxesSubplot at 0x904dc50>

In [25]:
# Load pricing data for a security
# start = '2013-01-01'
# end = '2015-01-01'
#x = get_pricing('reservoirH', fields='price', start_date=start, end_date=end)
x= df.reservoirH
# Construct a Kalman filter
kf = KalmanFilter(transition_matrices = [1],
                  observation_matrices = [1],
                  initial_state_mean = 39.3,
                  initial_state_covariance = 1,
                  observation_covariance=1,
                  transition_covariance=1)

# Use the observed values of the price to get a rolling mean
state_means, _ = kf.filter(x.values)

# Compute the rolling mean with various lookback windows
mean10 = pd.rolling_mean(x, 6)
mean20 = pd.rolling_mean(x, 20)
mean30 = pd.rolling_mean(x, 30)

# Plot original data and estimated mean
plt.plot(state_means)
plt.plot(x, 'k.', ms=2)
plt.plot(mean10)
plt.plot(mean20)
plt.plot(mean30)
plt.title('Kalman filter estimate of average')
plt.legend(['Kalman Estimate', 'Reseroir H', '30-day Moving Average', '60-day Moving Average','90-day Moving Average'])
plt.xlabel('Day')
plt.ylabel('Reservoir Level');



In [26]:
plt.plot(state_means)
plt.plot(x)
plt.title('Kalman filter estimate of average')
plt.legend(['Kalman Estimate', 'Reseroir H'])
plt.xlabel('Day')
plt.ylabel('Reservoir Level');


This is a little hard to see, so we'll plot a subsection of the graph.


In [27]:
plt.plot(state_means[-400:])
plt.plot(x[-400:])
plt.plot(mean10[-400:])
plt.title('Kalman filter estimate of average')
plt.legend(['Kalman Estimate', 'Reseroir H', '6-day Moving Average'])
plt.xlabel('Day')
plt.ylabel('Reservoir Level');



In [ ]:


In [28]:
# Load pricing data for a security
# start = '2013-01-01'
# end = '2015-01-01'
#x = get_pricing('reservoirH', fields='price', start_date=start, end_date=end)
xH= df.upperH_med
# Construct a Kalman filter
kf = KalmanFilter(transition_matrices = [1],
                  observation_matrices = [1],
                  initial_state_mean = 35.5,
                  initial_state_covariance = 1,
                  observation_covariance=1,
                  transition_covariance=.01)

# Use the observed values of the price to get a rolling mean
state_means, _ = kf.filter(xH.values)

# Compute the rolling mean with various lookback windows
mean10 = pd.rolling_mean(xH, 10)
mean20 = pd.rolling_mean(xH, 20)
mean30 = pd.rolling_mean(xH, 30)

# Plot original data and estimated mean
plt.plot(state_means)
plt.plot(xH)
plt.plot(mean10)
plt.plot(mean20)
plt.plot(mean30)
plt.title('Kalman filter estimate of average')
# plt.legend(['Kalman Estimate', 'upperH_med', '10-day Moving Average', '20-day Moving Average','30-day Moving Average'])
plt.xlabel('Day')
plt.ylabel('upperH_med');



In [208]:
txrxID =  df_DC.keys()[1:-1]
xmasking = lambda x: np.ma.masked_where(np.isnan(x.values), x.values)

In [242]:
x= df_DC[txrxID[150]]
median10 = pd.rolling_median(x, 6)
mean10 = pd.rolling_max(x, 3)
x1 = median10
x2 = mean10
# Masking array having NaN
xm = xmasking(x2)
# Construct a Kalman filter
kf = KalmanFilter(transition_matrices = [1],
                  observation_matrices = [1],
                  initial_state_mean = 67.6,
                  initial_state_covariance = 1,
                  observation_covariance=1,
                  transition_covariance=1)
# Use the observed values of the price to get a rolling mean
state_means, _ = kf.filter(xm)

In [243]:
#plt.plot(x1) 
plt.plot(x)
#plt.plot(x1) 
plt.plot(x2)
#plt.plot(state_means) 
plt.legend([ 'origin x',' max x2', 'Kalman Estimate'])


Out[243]:
<matplotlib.legend.Legend at 0x1636f5c0>

In [244]:
plt.plot(x)
plt.plot(state_means)


Out[244]:
[<matplotlib.lines.Line2D at 0x163e54a8>]

In [245]:
upperH_med = xmasking(df.upperH_med)
state_means, _ = kf.filter(upperH_med)

plt.plot(df.upperH_med)
plt.plot(state_means)

# plt.plot(xH)
# plt.plot(mean10)
plt.title('Kalman filter estimate of average')
plt.legend(['Kalman Estimate', 'Reseroir H','10-day Moving Average'])
plt.xlabel('Day')
plt.ylabel('upperH_med');



In [109]:
# Import libraries
%matplotlib inline
import pandas as pd
import sys

In [110]:
import matplotlib.pyplot as plt
import numpy as np
import scipy as sc

plt.style.use('ggplot')
np.random.seed(20)

In [184]:
#x = df.reservoirH
x =df_DC[txrxID[2]]

In [185]:
mean10 = pd.rolling_max(x, 4)

In [186]:
plt.plot(x)
plt.plot(mean10)


Out[186]:
[<matplotlib.lines.Line2D at 0x10ea89b0>]

In [135]:
#-------------------------------------------------------------------------------
# Set up

# Time
t = np.linspace(0,1,100)

# Frequencies in the signal
f1 = 20
f2 = 30

# Some random noise to add to the signal
noise = np.random.random_sample(len(t))

# Complete signal
y = x #2*np.sin(2*np.pi*f1*t+0.2) + 3*np.cos(2*np.pi*f2*t+0.3) + noise*5

# The part of the signal we want to isolate
y1 = x #2*np.sin(2*np.pi*f1*t+0.2)

In [ ]:


In [136]:
y


Out[136]:
0      67.69065
1      67.99035
2      68.25520
3      41.14775
4      13.76705
5      69.57090
6      69.72875
7      69.97565
8      70.15050
9      70.38895
10     70.47380
11     70.52660
12     70.71980
13     71.03690
14     71.00625
15     71.05955
16     71.12500
17     71.33380
18     71.57365
19     71.64240
20     71.85690
21     71.99350
22     72.00340
23     72.15140
24     72.23385
25     72.28900
26     72.34930
27     72.40260
28     72.60380
29     72.72585
         ...   
335         NaN
336         NaN
337         NaN
338         NaN
339         NaN
340         NaN
341         NaN
342         NaN
343         NaN
344         NaN
345         NaN
346         NaN
347         NaN
348         NaN
349         NaN
350         NaN
351    66.07475
352         NaN
353    66.23685
354    66.25100
355    66.06395
356    64.88725
357    64.24260
358    64.17610
359    64.17000
360    64.35285
361    64.45750
362    64.73035
363    64.83395
364    65.01635
Name: 4356, dtype: float64

In [137]:
# FFT of the signal
F = sc.fft(y)

# Other specs
N = len(t)                              # number of samples
dt = 0.001                              # inter-sample time difference
w = np.fft.fftfreq(N, dt)               # list of frequencies for the FFT
pFrequency = np.where(w>=0)[0]          # we only positive frequencies
magnitudeF = abs(F[:len(pFrequency)])   # magnitude of F for the positive frequencies

#-------------------------------------------------------------------------------
# Some functions we will need

In [138]:
plt.plot(F)


Out[138]:
[<matplotlib.lines.Line2D at 0xd776550>]

In [116]:
print F


[  1.16153914e+04 +0.00000000e+00j  -1.35725953e+02 -3.71578290e+03j
  -2.30503626e+02 -1.46764106e+03j  -1.15891991e+02 -9.16095740e+02j
  -6.38915857e+01 -8.94700809e+02j  -3.80955286e+01 -5.69654724e+02j
  -2.91970029e+01 -5.40230971e+02j  -2.37043823e+01 -4.55351692e+02j
   1.09157296e+00 -3.98882761e+02j   1.21556220e+01 -3.44408983e+02j
  -1.32490170e+01 -3.01779316e+02j  -1.79428798e+00 -3.01804129e+02j
   1.81734266e+01 -2.46973850e+02j  -5.45918758e+00 -2.37618131e+02j
   9.37053027e+00 -2.34423844e+02j   1.27883951e+01 -2.07009251e+02j
  -9.96018337e-02 -1.91390964e+02j   3.26257752e+00 -1.88068916e+02j
   7.23535070e+00 -1.72241393e+02j   1.07940335e+01 -1.56718415e+02j
  -1.26200858e+00 -1.58272701e+02j   1.93789927e+01 -1.56448579e+02j
   6.28658609e+00 -1.31073796e+02j   5.79802619e+00 -1.42763927e+02j
   1.52235451e+01 -1.26905662e+02j   6.03132614e+00 -1.25249565e+02j
   1.01772823e+01 -1.15284086e+02j   1.01147358e+01 -1.15819934e+02j
   1.03820530e+01 -1.11149910e+02j   9.51630656e+00 -1.05686618e+02j
   6.10749557e+00 -9.75018221e+01j   1.19258836e+01 -1.03540892e+02j
   1.38879912e+01 -9.36825432e+01j   7.82989907e+00 -9.39259163e+01j
   1.05451820e+01 -9.13490887e+01j   1.41479401e+01 -8.29489043e+01j
   4.81460722e+00 -8.57900416e+01j   1.07072355e+01 -8.27711952e+01j
   7.80184807e+00 -7.69149773e+01j   1.35411431e+01 -7.98728403e+01j
   9.20023320e+00 -7.76226403e+01j   1.29918748e+01 -7.27292625e+01j
   1.04544722e+01 -6.98766753e+01j   1.11788522e+01 -6.85480852e+01j
   8.58310575e+00 -6.94615106e+01j   9.92048142e+00 -6.53250909e+01j
   9.35270251e+00 -6.45164802e+01j   1.11042312e+01 -6.43657675e+01j
   1.25823429e+01 -6.36609954e+01j   1.11818023e+01 -5.83943016e+01j
   1.01661656e+01 -5.90417543e+01j   1.31104386e+01 -5.65920945e+01j
   9.12787137e+00 -5.54928332e+01j   1.04674618e+01 -5.63633397e+01j
   1.07742376e+01 -5.36969630e+01j   1.33592440e+01 -5.49395261e+01j
   1.04409174e+01 -5.05908149e+01j   1.09561104e+01 -5.13441736e+01j
   1.08152002e+01 -4.75225305e+01j   9.85155792e+00 -4.97118362e+01j
   1.09803341e+01 -4.74175282e+01j   1.01806342e+01 -4.81006676e+01j
   1.20439648e+01 -4.61956761e+01j   1.06959588e+01 -4.52842009e+01j
   1.08656515e+01 -4.49429010e+01j   1.06328076e+01 -4.23991079e+01j
   1.14452137e+01 -4.22636801e+01j   1.12227918e+01 -4.13733674e+01j
   1.14187088e+01 -4.18955856e+01j   1.15308330e+01 -3.93231623e+01j
   9.46780242e+00 -4.00684520e+01j   1.12486812e+01 -3.91580547e+01j
   1.07270686e+01 -3.82635960e+01j   1.07639632e+01 -3.62093508e+01j
   1.02810193e+01 -3.66844074e+01j   1.25487304e+01 -3.66953437e+01j
   1.09567398e+01 -3.38895381e+01j   9.68876786e+00 -3.58450297e+01j
   1.14862348e+01 -3.40684554e+01j   1.10594661e+01 -3.32442200e+01j
   1.09326588e+01 -3.30434272e+01j   1.09969799e+01 -3.24266567e+01j
   1.07637388e+01 -3.17502501e+01j   1.09501435e+01 -3.08041226e+01j
   9.53077131e+00 -3.09444361e+01j   1.25735813e+01 -3.03481706e+01j
   9.90680388e+00 -2.87861063e+01j   1.12264895e+01 -3.03344405e+01j
   1.21506855e+01 -2.90858632e+01j   1.12379201e+01 -2.80615083e+01j
   9.65033273e+00 -2.73676939e+01j   1.13005755e+01 -2.77129432e+01j
   1.07794448e+01 -2.61160442e+01j   1.06377307e+01 -2.66783961e+01j
   1.14778237e+01 -2.62635103e+01j   1.21035873e+01 -2.56897919e+01j
   1.16966993e+01 -2.45814672e+01j   1.08227662e+01 -2.45194824e+01j
   1.18090706e+01 -2.41034756e+01j   1.07650595e+01 -2.25860348e+01j
   9.89600915e+00 -2.41805305e+01j   1.12797317e+01 -2.29590505e+01j
   1.11997727e+01 -2.25124505e+01j   1.09729963e+01 -2.23354501e+01j
   1.11406024e+01 -2.17790133e+01j   1.11266946e+01 -2.05752915e+01j
   1.01303165e+01 -1.98050585e+01j   1.00036642e+01 -2.13144128e+01j
   1.13789720e+01 -2.07055685e+01j   1.01547521e+01 -1.92373985e+01j
   1.04140687e+01 -2.02230367e+01j   1.14876377e+01 -1.96871197e+01j
   1.10980506e+01 -1.81866582e+01j   1.06502975e+01 -1.77092295e+01j
   1.09566665e+01 -1.87095951e+01j   1.14455189e+01 -1.78534306e+01j
   1.06177734e+01 -1.72420094e+01j   1.08137947e+01 -1.74019368e+01j
   1.16105000e+01 -1.75938822e+01j   1.09897169e+01 -1.63767859e+01j
   9.80571400e+00 -1.65930980e+01j   1.20712170e+01 -1.57955166e+01j
   1.09419175e+01 -1.45969942e+01j   1.09401426e+01 -1.58815585e+01j
   1.12533340e+01 -1.49677896e+01j   1.12921978e+01 -1.49855904e+01j
   1.12147345e+01 -1.39209677e+01j   9.97815407e+00 -1.50029897e+01j
   1.20038793e+01 -1.45679390e+01j   1.09564374e+01 -1.23022067e+01j
   1.12983858e+01 -1.34807952e+01j   1.10755433e+01 -1.32147295e+01j
   1.07420264e+01 -1.27841952e+01j   1.08960602e+01 -1.23494313e+01j
   1.12934696e+01 -1.20282957e+01j   1.19893541e+01 -1.22963884e+01j
   1.11648358e+01 -1.14364568e+01j   1.05636482e+01 -1.15901700e+01j
   1.21892320e+01 -1.11687889e+01j   1.10005710e+01 -1.03421651e+01j
   9.98884856e+00 -1.06664946e+01j   1.19167250e+01 -1.05673412e+01j
   1.20442241e+01 -8.81073071e+00j   1.08731096e+01 -9.90536179e+00j
   1.17271676e+01 -9.70551980e+00j   1.09566881e+01 -8.47909141e+00j
   1.10262957e+01 -8.16552924e+00j   1.09521293e+01 -8.93373425e+00j
   1.10155188e+01 -8.77635562e+00j   1.12577169e+01 -6.68314899e+00j
   1.07267065e+01 -8.48101844e+00j   1.20285694e+01 -8.08900960e+00j
   1.09714254e+01 -6.03950779e+00j   1.07658114e+01 -6.91127225e+00j
   1.11563766e+01 -6.92760420e+00j   1.15897535e+01 -6.16412162e+00j
   1.03722051e+01 -6.52274301e+00j   1.11439401e+01 -6.81870605e+00j
   1.24830970e+01 -5.55821599e+00j   1.09231378e+01 -5.29594804e+00j
   1.07193530e+01 -5.58125393e+00j   1.22658728e+01 -4.90725358e+00j
   1.12155671e+01 -4.29172683e+00j   1.01686187e+01 -4.98191132e+00j
   1.17452181e+01 -4.97405672e+00j   1.12398008e+01 -3.54481822e+00j
   1.07872184e+01 -4.16841315e+00j   1.12461058e+01 -4.11186393e+00j
   1.13951499e+01 -3.65903055e+00j   1.13427732e+01 -2.47964064e+00j
   1.08530526e+01 -2.96204074e+00j   1.17566552e+01 -3.05316143e+00j
   1.12077114e+01 -1.70405930e+00j   1.05354846e+01 -3.32380280e+00j
   1.20509136e+01 -2.35490711e+00j   1.11822479e+01 -1.23616490e+00j
   1.09884754e+01 -1.36789997e+00j   1.11391644e+01 -9.36233138e-01j
   1.17558285e+01 -8.13417168e-01j   1.09660618e+01 -2.18960998e-01j
   1.04559352e+01 -8.85598626e-01j   1.15378692e+01 -8.42504229e-01j
   1.10619539e+01 +4.71104897e-01j   1.10619539e+01 -4.71104897e-01j
   1.15378692e+01 +8.42504229e-01j   1.04559352e+01 +8.85598626e-01j
   1.09660618e+01 +2.18960998e-01j   1.17558285e+01 +8.13417168e-01j
   1.11391644e+01 +9.36233138e-01j   1.09884754e+01 +1.36789997e+00j
   1.11822479e+01 +1.23616490e+00j   1.20509136e+01 +2.35490711e+00j
   1.05354846e+01 +3.32380280e+00j   1.12077114e+01 +1.70405930e+00j
   1.17566552e+01 +3.05316143e+00j   1.08530526e+01 +2.96204074e+00j
   1.13427732e+01 +2.47964064e+00j   1.13951499e+01 +3.65903055e+00j
   1.12461058e+01 +4.11186393e+00j   1.07872184e+01 +4.16841315e+00j
   1.12398008e+01 +3.54481822e+00j   1.17452181e+01 +4.97405672e+00j
   1.01686187e+01 +4.98191132e+00j   1.12155671e+01 +4.29172683e+00j
   1.22658728e+01 +4.90725358e+00j   1.07193530e+01 +5.58125393e+00j
   1.09231378e+01 +5.29594804e+00j   1.24830970e+01 +5.55821599e+00j
   1.11439401e+01 +6.81870605e+00j   1.03722051e+01 +6.52274301e+00j
   1.15897535e+01 +6.16412162e+00j   1.11563766e+01 +6.92760420e+00j
   1.07658114e+01 +6.91127225e+00j   1.09714254e+01 +6.03950779e+00j
   1.20285694e+01 +8.08900960e+00j   1.07267065e+01 +8.48101844e+00j
   1.12577169e+01 +6.68314899e+00j   1.10155188e+01 +8.77635562e+00j
   1.09521293e+01 +8.93373425e+00j   1.10262957e+01 +8.16552924e+00j
   1.09566881e+01 +8.47909141e+00j   1.17271676e+01 +9.70551980e+00j
   1.08731096e+01 +9.90536179e+00j   1.20442241e+01 +8.81073071e+00j
   1.19167250e+01 +1.05673412e+01j   9.98884856e+00 +1.06664946e+01j
   1.10005710e+01 +1.03421651e+01j   1.21892320e+01 +1.11687889e+01j
   1.05636482e+01 +1.15901700e+01j   1.11648358e+01 +1.14364568e+01j
   1.19893541e+01 +1.22963884e+01j   1.12934696e+01 +1.20282957e+01j
   1.08960602e+01 +1.23494313e+01j   1.07420264e+01 +1.27841952e+01j
   1.10755433e+01 +1.32147295e+01j   1.12983858e+01 +1.34807952e+01j
   1.09564374e+01 +1.23022067e+01j   1.20038793e+01 +1.45679390e+01j
   9.97815407e+00 +1.50029897e+01j   1.12147345e+01 +1.39209677e+01j
   1.12921978e+01 +1.49855904e+01j   1.12533340e+01 +1.49677896e+01j
   1.09401426e+01 +1.58815585e+01j   1.09419175e+01 +1.45969942e+01j
   1.20712170e+01 +1.57955166e+01j   9.80571400e+00 +1.65930980e+01j
   1.09897169e+01 +1.63767859e+01j   1.16105000e+01 +1.75938822e+01j
   1.08137947e+01 +1.74019368e+01j   1.06177734e+01 +1.72420094e+01j
   1.14455189e+01 +1.78534306e+01j   1.09566665e+01 +1.87095951e+01j
   1.06502975e+01 +1.77092295e+01j   1.10980506e+01 +1.81866582e+01j
   1.14876377e+01 +1.96871197e+01j   1.04140687e+01 +2.02230367e+01j
   1.01547521e+01 +1.92373985e+01j   1.13789720e+01 +2.07055685e+01j
   1.00036642e+01 +2.13144128e+01j   1.01303165e+01 +1.98050585e+01j
   1.11266946e+01 +2.05752915e+01j   1.11406024e+01 +2.17790133e+01j
   1.09729963e+01 +2.23354501e+01j   1.11997727e+01 +2.25124505e+01j
   1.12797317e+01 +2.29590505e+01j   9.89600915e+00 +2.41805305e+01j
   1.07650595e+01 +2.25860348e+01j   1.18090706e+01 +2.41034756e+01j
   1.08227662e+01 +2.45194824e+01j   1.16966993e+01 +2.45814672e+01j
   1.21035873e+01 +2.56897919e+01j   1.14778237e+01 +2.62635103e+01j
   1.06377307e+01 +2.66783961e+01j   1.07794448e+01 +2.61160442e+01j
   1.13005755e+01 +2.77129432e+01j   9.65033273e+00 +2.73676939e+01j
   1.12379201e+01 +2.80615083e+01j   1.21506855e+01 +2.90858632e+01j
   1.12264895e+01 +3.03344405e+01j   9.90680388e+00 +2.87861063e+01j
   1.25735813e+01 +3.03481706e+01j   9.53077131e+00 +3.09444361e+01j
   1.09501435e+01 +3.08041226e+01j   1.07637388e+01 +3.17502501e+01j
   1.09969799e+01 +3.24266567e+01j   1.09326588e+01 +3.30434272e+01j
   1.10594661e+01 +3.32442200e+01j   1.14862348e+01 +3.40684554e+01j
   9.68876786e+00 +3.58450297e+01j   1.09567398e+01 +3.38895381e+01j
   1.25487304e+01 +3.66953437e+01j   1.02810193e+01 +3.66844074e+01j
   1.07639632e+01 +3.62093508e+01j   1.07270686e+01 +3.82635960e+01j
   1.12486812e+01 +3.91580547e+01j   9.46780242e+00 +4.00684520e+01j
   1.15308330e+01 +3.93231623e+01j   1.14187088e+01 +4.18955856e+01j
   1.12227918e+01 +4.13733674e+01j   1.14452137e+01 +4.22636801e+01j
   1.06328076e+01 +4.23991079e+01j   1.08656515e+01 +4.49429010e+01j
   1.06959588e+01 +4.52842009e+01j   1.20439648e+01 +4.61956761e+01j
   1.01806342e+01 +4.81006676e+01j   1.09803341e+01 +4.74175282e+01j
   9.85155792e+00 +4.97118362e+01j   1.08152002e+01 +4.75225305e+01j
   1.09561104e+01 +5.13441736e+01j   1.04409174e+01 +5.05908149e+01j
   1.33592440e+01 +5.49395261e+01j   1.07742376e+01 +5.36969630e+01j
   1.04674618e+01 +5.63633397e+01j   9.12787137e+00 +5.54928332e+01j
   1.31104386e+01 +5.65920945e+01j   1.01661656e+01 +5.90417543e+01j
   1.11818023e+01 +5.83943016e+01j   1.25823429e+01 +6.36609954e+01j
   1.11042312e+01 +6.43657675e+01j   9.35270251e+00 +6.45164802e+01j
   9.92048142e+00 +6.53250909e+01j   8.58310575e+00 +6.94615106e+01j
   1.11788522e+01 +6.85480852e+01j   1.04544722e+01 +6.98766753e+01j
   1.29918748e+01 +7.27292625e+01j   9.20023320e+00 +7.76226403e+01j
   1.35411431e+01 +7.98728403e+01j   7.80184807e+00 +7.69149773e+01j
   1.07072355e+01 +8.27711952e+01j   4.81460722e+00 +8.57900416e+01j
   1.41479401e+01 +8.29489043e+01j   1.05451820e+01 +9.13490887e+01j
   7.82989907e+00 +9.39259163e+01j   1.38879912e+01 +9.36825432e+01j
   1.19258836e+01 +1.03540892e+02j   6.10749557e+00 +9.75018221e+01j
   9.51630656e+00 +1.05686618e+02j   1.03820530e+01 +1.11149910e+02j
   1.01147358e+01 +1.15819934e+02j   1.01772823e+01 +1.15284086e+02j
   6.03132614e+00 +1.25249565e+02j   1.52235451e+01 +1.26905662e+02j
   5.79802619e+00 +1.42763927e+02j   6.28658609e+00 +1.31073796e+02j
   1.93789927e+01 +1.56448579e+02j  -1.26200858e+00 +1.58272701e+02j
   1.07940335e+01 +1.56718415e+02j   7.23535070e+00 +1.72241393e+02j
   3.26257752e+00 +1.88068916e+02j  -9.96018337e-02 +1.91390964e+02j
   1.27883951e+01 +2.07009251e+02j   9.37053027e+00 +2.34423844e+02j
  -5.45918758e+00 +2.37618131e+02j   1.81734266e+01 +2.46973850e+02j
  -1.79428798e+00 +3.01804129e+02j  -1.32490170e+01 +3.01779316e+02j
   1.21556220e+01 +3.44408983e+02j   1.09157296e+00 +3.98882761e+02j
  -2.37043823e+01 +4.55351692e+02j  -2.91970029e+01 +5.40230971e+02j
  -3.80955286e+01 +5.69654724e+02j  -6.38915857e+01 +8.94700809e+02j
  -1.15891991e+02 +9.16095740e+02j  -2.30503626e+02 +1.46764106e+03j
  -1.35725953e+02 +3.71578290e+03j]

In [139]:
# Plots the FFT
def pltfft():
    plt.plot(pFrequency,magnitudeF)
    plt.xlabel('Hz')
    plt.ylabel('Magnitude')
    plt.title('FFT of the full signal')
    plt.grid(True)
    plt.show()

# Plots the full signal
def pltCompleteSignal():
    plt.plot(t,y,'b')
    plt.xlabel('Time (s)')
    plt.ylabel('Amplitude')
    plt.title('Full signal')
    plt.grid(True)
    plt.show()

In [ ]:


In [140]:
# Filter function:
# blocks higher frequency than fmax, lower than fmin and returns the cleaned FT
def blockHigherFreq(FT,fmin,fmax,plot=False):
    for i in range(len(F)):
        if (i>= fmax) or (i<=fmin):
            FT[i] = 0
    if plot:
        plt.plot(pFrequency,abs(FT[:len(pFrequency)]))
        plt.xlabel('Hz')
        plt.ylabel('Magnitude')
        plt.title('Cleaned FFT')
        plt.grid(True)
        plt.show()
    return FT

# Normalising function (gets the signal in a scale from 0 to 1)
def normalise(signal):
    M = max(signal)
    normalised = signal/M
    return normalised

In [141]:
plt.plot(y)
plt.plot(y1)


Out[141]:
[<matplotlib.lines.Line2D at 0xde387b8>]

In [145]:
print F


[  0. +0.j   0. +0.j   0. +0.j   0. +0.j   0. +0.j   0. +0.j   0. +0.j
   0. +0.j   0. +0.j   0. +0.j   0. +0.j   0. +0.j   0. +0.j   0. +0.j
   0. +0.j   0. +0.j   0. +0.j   0. +0.j   0. +0.j  nan+nanj  nan+nanj
  nan+nanj   0. +0.j   0. +0.j   0. +0.j   0. +0.j   0. +0.j   0. +0.j
   0. +0.j   0. +0.j   0. +0.j   0. +0.j   0. +0.j   0. +0.j   0. +0.j
   0. +0.j   0. +0.j   0. +0.j   0. +0.j   0. +0.j   0. +0.j   0. +0.j
   0. +0.j   0. +0.j   0. +0.j   0. +0.j   0. +0.j   0. +0.j   0. +0.j
   0. +0.j   0. +0.j   0. +0.j   0. +0.j   0. +0.j   0. +0.j   0. +0.j
   0. +0.j   0. +0.j   0. +0.j   0. +0.j   0. +0.j   0. +0.j   0. +0.j
   0. +0.j   0. +0.j   0. +0.j   0. +0.j   0. +0.j   0. +0.j   0. +0.j
   0. +0.j   0. +0.j   0. +0.j   0. +0.j   0. +0.j   0. +0.j   0. +0.j
   0. +0.j   0. +0.j   0. +0.j   0. +0.j   0. +0.j   0. +0.j   0. +0.j
   0. +0.j   0. +0.j   0. +0.j   0. +0.j   0. +0.j   0. +0.j   0. +0.j
   0. +0.j   0. +0.j   0. +0.j   0. +0.j   0. +0.j   0. +0.j   0. +0.j
   0. +0.j   0. +0.j   0. +0.j   0. +0.j   0. +0.j   0. +0.j   0. +0.j
   0. +0.j   0. +0.j   0. +0.j   0. +0.j   0. +0.j   0. +0.j   0. +0.j
   0. +0.j   0. +0.j   0. +0.j   0. +0.j   0. +0.j   0. +0.j   0. +0.j
   0. +0.j   0. +0.j   0. +0.j   0. +0.j   0. +0.j   0. +0.j   0. +0.j
   0. +0.j   0. +0.j   0. +0.j   0. +0.j   0. +0.j   0. +0.j   0. +0.j
   0. +0.j   0. +0.j   0. +0.j   0. +0.j   0. +0.j   0. +0.j   0. +0.j
   0. +0.j   0. +0.j   0. +0.j   0. +0.j   0. +0.j   0. +0.j   0. +0.j
   0. +0.j   0. +0.j   0. +0.j   0. +0.j   0. +0.j   0. +0.j   0. +0.j
   0. +0.j   0. +0.j   0. +0.j   0. +0.j   0. +0.j   0. +0.j   0. +0.j
   0. +0.j   0. +0.j   0. +0.j   0. +0.j   0. +0.j   0. +0.j   0. +0.j
   0. +0.j   0. +0.j   0. +0.j   0. +0.j   0. +0.j   0. +0.j   0. +0.j
   0. +0.j   0. +0.j   0. +0.j   0. +0.j   0. +0.j   0. +0.j   0. +0.j
   0. +0.j   0. +0.j   0. +0.j   0. +0.j   0. +0.j   0. +0.j   0. +0.j
   0. +0.j   0. +0.j   0. +0.j   0. +0.j   0. +0.j   0. +0.j   0. +0.j
   0. +0.j   0. +0.j   0. +0.j   0. +0.j   0. +0.j   0. +0.j   0. +0.j
   0. +0.j   0. +0.j   0. +0.j   0. +0.j   0. +0.j   0. +0.j   0. +0.j
   0. +0.j   0. +0.j   0. +0.j   0. +0.j   0. +0.j   0. +0.j   0. +0.j
   0. +0.j   0. +0.j   0. +0.j   0. +0.j   0. +0.j   0. +0.j   0. +0.j
   0. +0.j   0. +0.j   0. +0.j   0. +0.j   0. +0.j   0. +0.j   0. +0.j
   0. +0.j   0. +0.j   0. +0.j   0. +0.j   0. +0.j   0. +0.j   0. +0.j
   0. +0.j   0. +0.j   0. +0.j   0. +0.j   0. +0.j   0. +0.j   0. +0.j
   0. +0.j   0. +0.j   0. +0.j   0. +0.j   0. +0.j   0. +0.j   0. +0.j
   0. +0.j   0. +0.j   0. +0.j   0. +0.j   0. +0.j   0. +0.j   0. +0.j
   0. +0.j   0. +0.j   0. +0.j   0. +0.j   0. +0.j   0. +0.j   0. +0.j
   0. +0.j   0. +0.j   0. +0.j   0. +0.j   0. +0.j   0. +0.j   0. +0.j
   0. +0.j   0. +0.j   0. +0.j   0. +0.j   0. +0.j   0. +0.j   0. +0.j
   0. +0.j   0. +0.j   0. +0.j   0. +0.j   0. +0.j   0. +0.j   0. +0.j
   0. +0.j   0. +0.j   0. +0.j   0. +0.j   0. +0.j   0. +0.j   0. +0.j
   0. +0.j   0. +0.j   0. +0.j   0. +0.j   0. +0.j   0. +0.j   0. +0.j
   0. +0.j   0. +0.j   0. +0.j   0. +0.j   0. +0.j   0. +0.j   0. +0.j
   0. +0.j   0. +0.j   0. +0.j   0. +0.j   0. +0.j   0. +0.j   0. +0.j
   0. +0.j   0. +0.j   0. +0.j   0. +0.j   0. +0.j   0. +0.j   0. +0.j
   0. +0.j   0. +0.j   0. +0.j   0. +0.j   0. +0.j   0. +0.j   0. +0.j
   0. +0.j   0. +0.j   0. +0.j   0. +0.j   0. +0.j   0. +0.j   0. +0.j
   0. +0.j   0. +0.j   0. +0.j   0. +0.j   0. +0.j   0. +0.j   0. +0.j
   0. +0.j   0. +0.j   0. +0.j   0. +0.j   0. +0.j   0. +0.j   0. +0.j
   0. +0.j   0. +0.j   0. +0.j   0. +0.j   0. +0.j   0. +0.j   0. +0.j
   0. +0.j   0. +0.j   0. +0.j   0. +0.j   0. +0.j   0. +0.j   0. +0.j
   0. +0.j]

In [142]:
#-------------------------------------------------------------------------------
# Processing

# Cleaning the FT by selecting only frequencies between 18 and 22
newFT = blockHigherFreq(F,18,22)

# Getting back the cleaned signal
cleanedSignal = sc.ifft(F)

# Error
error = normalise(y1) - normalise(cleanedSignal)

In [143]:
plt.plot(y,'g')


Out[143]:
[<matplotlib.lines.Line2D at 0xa5407b8>]

In [144]:
#
#-------------------------------------------------------------------------------
# Plot the findings

#pltCompleteSignal()         #Plot the full signal
#pltfft()                    #Plot fft

plt.figure()

plt.subplot(3,1,1)          #Subplot 1
plt.title('Original signal')
plt.plot(y,'g')

plt.subplot(3,1,2)          #Subplot 2
plt.plot(normalise(cleanedSignal),label='Cleaned signal',color='b')
plt.plot(normalise(y1),label='Signal to find',ls='-',color='r')
plt.title('Cleaned signal and signal to find')
plt.legend()

plt.subplot(3,1,3)          #Subplot 3
plt.plot(error,color='r',label='error')
plt.show()



In [ ]:


In [92]:
% Signal parameters:
#f = [ 440 880 1000 2000 ];      % frequencies
#M = 256;                        % signal length
#Fs = 5000;                      % sampling rate

% Generate a signal by adding up sinusoids:
x = zeros(1,M); % pre-allocate 'accumulator'
n = 0:(M-1);    % discrete-time grid
#for fk = f;
    x = df.reservoirH ;
#end


  File "<ipython-input-92-2dd45d0bfe9c>", line 7
    x = zeros(1,M); % pre-allocate 'accumulator'
                    ^
SyntaxError: invalid syntax

In [125]:
plt. plot(sc.ifft(F))


Out[125]:
[<matplotlib.lines.Line2D at 0xcfbd438>]

In [94]:
yy = df.reservoirH 
    #yyy = df.reservoirH   
    print x


  File "<ipython-input-94-e52c1a9e912b>", line 3
    print x
    ^
IndentationError: unexpected indent

In [72]:
from scipy.fftpack import fft
# Number of sample points
N = 364
# sample spacing
T = 1.0
x = np.linspace(1, N*T, N)
#y = df.reservoirH*x #np.sin(50.0 * 2.0*np.pi*x) + 0.5*np.sin(80.0 * 2.0*np.pi*x)
y = np.sin(50.0 * 2.0*np.pi*x) + 0.5*np.sin(80.0 * 2.0*np.pi*x)

yf = fft(y)
from scipy.signal import blackman
w = blackman(N)
ywf = fft(y*w)
xf = np.linspace(0.0, 1.0/(2.0*T), N/2)
import matplotlib.pyplot as plt
plt.semilogy(xf[1:N/2], 2.0/N * np.abs(yf[1:N/2]), '-b')
plt.semilogy(xf[1:N/2], 2.0/N * np.abs(ywf[1:N/2]), '-r')
plt.legend(['FFT', 'FFT w. window'])
plt.grid()
plt.show()



In [73]:
plt.plot(y)
plt.plot(yf)


Out[73]:
[<matplotlib.lines.Line2D at 0xbb67cf8>]

In [74]:
print y


[ -7.83278767e-15  -1.56655753e-14  -1.37185201e-13  -3.13311507e-14
   7.45228994e-14  -2.74370401e-13   5.88573240e-14  -6.26623014e-14
  -1.84181927e-13   1.49045799e-13   2.75261734e-14  -5.48740803e-13
  -2.15513077e-13   1.17714648e-13  -4.58552328e-13  -1.25324603e-13
   2.07903123e-13  -3.68363853e-13  -3.51361280e-14   2.98091597e-13
  -2.78175379e-13   5.50523467e-14   3.88280072e-13  -1.09748161e-12
  -7.64253880e-13  -4.31026155e-13  -9.77984294e-14   2.35429296e-13
  -1.25033238e-12  -9.17104656e-13  -5.83876931e-13  -2.50649205e-13
  -8.26916182e-13   4.15806246e-13  -1.60460731e-13  -7.36727707e-13
  -1.31299468e-12  -7.02722560e-14  -6.46539232e-13   5.96183195e-13
   1.99162187e-14  -5.56350758e-13  -1.13261773e-12   1.10104693e-13
  -4.66162283e-13   7.76560144e-13   2.00293168e-13  -2.19496321e-12
  -9.52240784e-13  -1.52850776e-12  -2.85785333e-13  -8.62052310e-13
   3.80670118e-13  -1.95596859e-13   1.04712557e-12   4.70858592e-13
   1.71358102e-12  -2.50066476e-12  -1.25794234e-12  -1.83420931e-12
  -5.91486886e-13  -1.16775386e-12   7.49685654e-14  -5.01298411e-13
  -1.07756539e-12  -1.65383236e-12  -4.11109936e-13   8.31612491e-13
   2.07433492e-12  -3.20921461e-13  -2.71617784e-12  -1.47345541e-12
  -2.30732987e-13  -2.62598937e-12  -1.38326694e-12  -1.40544512e-13
   1.10217792e-12  -1.29307846e-12  -5.03560373e-14   1.19236639e-12
  -1.20288999e-12   3.98324374e-14   1.28255486e-12  -1.11270152e-12
  -3.50795789e-12  -2.26523547e-12  -1.02251304e-12   2.20209387e-13
  -2.17504699e-12  -9.32324566e-13   3.10397862e-13   1.55312029e-12
  -8.42136091e-13   4.00586336e-13   1.64330876e-12  -4.38992642e-12
  -3.14720400e-12  -1.90448157e-12  -6.61759142e-13  -3.05701552e-12
  -1.81429309e-12  -5.71570667e-13   6.71151760e-13  -1.72410462e-12
  -4.11936100e-12   7.61340235e-13  -1.63391614e-12  -3.91193717e-13
  -2.78645010e-12   2.09425114e-12  -3.01005243e-13   9.41717185e-13
  -1.45353920e-12   3.42716204e-12  -2.60607315e-12  -5.00132953e-12
  -1.20628293e-13  -2.51588467e-12  -1.27316225e-12  -3.66841863e-12
   1.21228261e-12  -1.18297377e-12   5.97486562e-14  -2.33550772e-12
   2.54519351e-12   1.49937131e-13   1.39265956e-12  -1.00259682e-12
  -3.39785320e-12  -2.15513077e-12  -4.55038715e-12  -3.30766473e-12
  -2.06494230e-12  -8.22219872e-13   4.20502555e-13   1.66322498e-12
  -4.37001020e-12   4.14866984e-12  -1.88456535e-12  -6.41842923e-13
   6.00879504e-13  -5.43235568e-12   3.08632436e-12  -2.94691083e-12
  -1.70418840e-12  -4.61465973e-13   7.81256454e-13  -5.25197873e-12
   3.26670131e-12  -2.76653388e-12  -1.52381145e-12  -2.81089024e-13
   9.61633403e-13   2.20435583e-12  -3.82887936e-12  -2.58615693e-12
  -1.34343450e-12  -1.00712075e-13  -6.13394726e-12   2.38473278e-12
  -3.64850241e-12  -2.40577998e-12  -1.16305755e-12   7.96648749e-14
   1.32238730e-12   2.56510973e-12  -3.46812546e-12  -2.22540303e-12
  -9.82680603e-13  -7.01591579e-12   1.50276425e-12  -4.53047094e-12
   3.98820911e-12  -2.04502608e-12  -8.02303654e-13   4.40418774e-13
   1.68314120e-12  -4.35009399e-12   4.16858606e-12  -1.86464913e-12
  -7.89788432e-12   6.20795723e-13  -5.41243946e-12   3.10624058e-12
  -2.92699461e-12  -1.68427218e-12  -4.41549755e-13   8.01172673e-13
  -5.23206251e-12   3.28661753e-12  -2.74661766e-12  -8.77985285e-12
  -2.61172805e-13  -6.29440799e-12   2.22427205e-12  -3.80896314e-12
  -2.56624071e-12  -1.32351828e-12  -8.07958558e-14  -6.11403104e-12
   2.40464900e-12  -3.62858619e-12   4.89009385e-12  -1.14314133e-12
  -7.17637652e-12   1.34230352e-12  -4.69093167e-12  -3.44820924e-12
  -2.20548681e-12  -8.23872200e-12   2.79958043e-13   1.52268047e-12
  -4.51055472e-12  -3.26783229e-12   5.25084775e-12  -7.82387435e-13
   4.60334992e-13  -5.57290019e-12  -1.16061354e-11   4.18850227e-12
  -1.84473291e-12  -6.02010485e-13  -6.63524567e-12   1.88343437e-12
   3.12615680e-12  -2.90707839e-12  -8.94031358e-12   6.85432408e-12
   8.21088891e-13  -5.21214630e-12  -3.96942387e-12  -1.00026591e-11
   5.79197860e-12  -2.41256587e-13  -6.27449177e-12  -5.03176935e-12
   3.48691070e-12  -2.54632449e-12  -1.30360206e-12  -7.33683725e-12
   1.18184279e-12   2.42456522e-12  -3.60866997e-12  -2.36594754e-12
  -8.39918273e-12   1.19497312e-13   1.36221974e-12  -4.67101545e-12
  -1.07042506e-11   5.09038702e-12  -9.42848166e-13   2.99874262e-13
  -5.73336093e-12   2.78531912e-12   4.02804154e-12  -2.00519364e-12
  -8.03842883e-12  -6.79570640e-12   1.72297364e-12  -4.31026155e-12
  -3.06753912e-12  -9.10077431e-12  -5.82094267e-13  -6.61532945e-12
   1.90335059e-12  -4.12988460e-12   4.38879544e-12  -1.64443974e-12
  -7.67767493e-12   8.41005110e-13  -5.19223008e-12   3.32644996e-12
  -2.70678522e-12  -8.74002041e-12  -1.47732556e-11   8.29733967e-12
   2.26410449e-12  -3.76913070e-12  -9.80236589e-12  -1.28368585e-12
   7.23499420e-12   1.20175901e-12  -4.83147618e-12  -1.08647114e-11
  -2.34603132e-12   6.17264872e-12   1.39413531e-13  -5.89382166e-12
   2.62485839e-12  -3.40837680e-12  -9.44161199e-12  -9.22931947e-13
   7.59574809e-12   1.56251291e-12  -4.47072228e-12  -1.05039575e-11
  -1.98527742e-12   6.53340262e-12   5.00167430e-13  -5.53306776e-12
  -1.15663029e-11  -3.04762290e-12   5.47105714e-12  -5.62178048e-13
  -6.59541323e-12   1.92326681e-12  -4.10996838e-12   4.40871166e-12
  -1.62452353e-12  -7.65775871e-12   8.60921329e-13  -5.17231386e-12
  -1.12055490e-11  -2.68686900e-12   5.83181104e-12  -2.01424149e-13
  -6.23465934e-12  -1.22678945e-11   1.08027007e-11   4.76946556e-12
  -1.26376963e-12  -7.29700481e-12  -1.33302400e-11  -4.81155996e-12
   3.70712008e-12  -2.32611510e-12  -8.35935029e-12   1.59329750e-13
  -5.87390544e-12   2.64477460e-12  -3.38846058e-12   5.13021946e-12
  -9.03015728e-13  -6.93625092e-12   1.58242913e-12  -4.45080606e-12
   4.06787398e-12  -1.96536121e-12  -7.99859639e-12  -1.40318316e-11
   9.03876369e-12   3.00552850e-12  -3.02770668e-12  -9.06094187e-12
  -5.42261829e-13   7.97641821e-12   1.94318303e-12  -4.09005216e-12
  -1.01232873e-11  -1.60460731e-12  -7.63784249e-12   8.80837547e-13
  -5.15239764e-12   3.36628240e-12  -2.66695278e-12  -8.70018797e-12
  -1.81507930e-13   8.33717211e-12   2.30393692e-12  -3.72929826e-12
  -9.76253345e-12  -1.57957686e-11   7.27482663e-12   1.24159145e-12]

In [60]:
from scipy.fftpack import dct, idct
import matplotlib.pyplot as plt
N = 100
t = np.linspace(0,20,N)
x = np.exp(-t/3)*np.cos(2*t)
y = dct(x, norm='ortho')
window = np.zeros(N)
window[:20] = 1
yr = idct(y*window, norm='ortho')
sum(abs(x-yr)**2) / sum(abs(x)**2)
# 0.0010901402257
plt.plot(t, x, '-bx')
plt.plot(t, yr, 'ro')
window = np.zeros(N)
window[:15] = 1
yr = idct(y*window, norm='ortho')
sum(abs(x-yr)**2) / sum(abs(x)**2)
# 0.0718818065008
plt.plot(t, yr, 'g+')
plt.legend(['x', '$x_{20}$', '$x_{15}$'])
plt.grid()
plt.show()



In [70]:
from scipy.fftpack import dct, idct
import matplotlib.pyplot as plt
N = 364
t = np.linspace(0,1,N)
x = df.reservoirH #np.exp(-t/3)*np.cos(2*t)
y = dct(x, norm='ortho')
window = np.zeros(N)
window[:20] = 1
yr = idct(y*window, norm='ortho')
sum(abs(x-yr)**2) / sum(abs(x)**2)
# 0.0010901402257
plt.plot(t, x, '-bx')
plt.plot(t, yr, 'ro')
window = np.zeros(N)
window[:15] = 1
yr = idct(y*window, norm='ortho')
sum(abs(x-yr)**2) / sum(abs(x)**2)
# 0.0718818065008
plt.plot(t, yr, 'g+')
plt.legend(['x', '$x_{20}$', '$x_{15}$'])
plt.grid()
plt.show()


---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-70-7d4a69ad7a6d> in <module>()
      7 window = np.zeros(N)
      8 window[:20] = 1
----> 9 yr = idct(y*window, norm='ortho')
     10 sum(abs(x-yr)**2) / sum(abs(x)**2)
     11 # 0.0010901402257

ValueError: operands could not be broadcast together with shapes (365,) (364,) 

In [ ]:


In [ ]: